package com.example.controller;

import com.example.common.Result;
import com.example.entity.Comment;
import com.example.service.CommentService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;
    /**
     * 查询所有分类
     * @return
     */
    @GetMapping("/selectAll")
    public Result selectAll(Comment comment) {
        List<Comment> comments = commentService.selectAll(comment);
        return Result.success(comments);
    }

    /**
     * 分页查询分类
     * @param pageNum
     * @param pageSize
     * @return 分页结果
     */
    @GetMapping("/selectByPage")
    public Result selectByPage(Comment comment, @RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Comment> CommentPageInfo = commentService.selectByPage(comment, pageNum, pageSize);
        return Result.success(CommentPageInfo);
    }

    /**
     * 新增分类
     * @param comment
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Comment comment) {
        commentService.add(comment);
        return Result.success();
    }

    /**
     * 修改分类
     * @param comment
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody Comment comment) {
        commentService.update(comment);
        return Result.success();
    }

    /**
     * 删除分类
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        commentService.delete(id);
        return Result.success();
    }
    /**
     * 批量删除评论
     * @param ids
     * @return
     */
    @DeleteMapping("/batchDelete")
    public Result batchDelete(@RequestBody List<Integer> ids) {
        commentService.batchDelete(ids);
        return Result.success();
    }
}
